package com.fany.fastsearcher.activity;

import android.os.Bundle;
import android.widget.SimpleAdapter;

import com.fany.constants.StringResources;
import com.fany.fastsearcher.R;
import com.fany.utils.FileHelper;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

public class FileDetailActivity extends ListViewActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        String path = getIntent().getStringExtra("path");
        if (FileHelper.isDirectory(path))
        {
            loadDirectoryInfo(path);
        }
        else
        {
            loadFileInfo(path);
        }
    }

    private void loadFileInfo(String path)
    {
        File file = new File(path);
        ArrayList<HashMap<String, Object>> items = new ArrayList<HashMap<String, Object>>();
        items.add(makeProperty(StringResources.FileName, file.getName()));
        items.add(makeProperty(StringResources.FullPath, file.getAbsolutePath()));
        items.add(makeProperty(StringResources.FileSize, Long.toString(file.length() / 1024) + " KB"));
        items.add(makeProperty(StringResources.FileType, FileHelper.getTypeByFileExtension(FileHelper.extractFileExtension(path))));
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        items.add(makeProperty(StringResources.FileModifyTime, format.format(new Date(file.lastModified()))));
        items.add(makeProperty(StringResources.IsWritable, file.canWrite() ? "是" : "否"));
        items.add(makeProperty(StringResources.IsReadable, file.canRead() ? "是" : "否"));
        items.add(makeProperty(StringResources.IsHidden, file.isHidden() ? "是" : "否"));


        setAdapter(new SimpleAdapter(this, items,
                R.layout.filedetail_liststyle, new String[]{"key", "value"},
                new int[]{R.id.text_property_key, R.id.text_property_value}));
    }

    private void loadDirectoryInfo(String path)
    {
        File file = new File(path);
        ArrayList<HashMap<String, Object>> items = new ArrayList<HashMap<String, Object>>();
        items.add(makeProperty(StringResources.FileName, file.getName()));
        items.add(makeProperty(StringResources.FullPath, file.getAbsolutePath()));
        items.add(makeProperty(StringResources.FileType, "目录"));
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        items.add(makeProperty(StringResources.FileModifyTime, format.format(new Date(file.lastModified()))));
        items.add(makeProperty(StringResources.IsWritable, file.canWrite() ? "是" : "否"));
        items.add(makeProperty(StringResources.IsReadable, file.canRead() ? "是" : "否"));
        items.add(makeProperty(StringResources.IsHidden, file.isHidden() ? "是" : "否"));
        File[] files = file.listFiles();
        int fileCount = 0;
        int directoryCount = 0;
        if (files != null)
        {
            for (File child : files)
            {
                if (child.isDirectory())
                {
                    directoryCount++;
                }
                else
                {
                    fileCount++;
                }
            }
        }
        items.add(makeProperty("包含", String.format("%d 个文件，%d 个文件夹", fileCount, directoryCount)));

        setAdapter(new SimpleAdapter(this, items,
                R.layout.filedetail_liststyle, new String[]{"key", "value"},
                new int[]{R.id.text_property_key, R.id.text_property_value}));
    }

    private HashMap<String, Object> makeProperty(String key, String value)
    {
        HashMap<String, Object> item = new HashMap<String, Object>();
        item.put("key", key);
        item.put("value", value);
        return item;
    }

    @Override
    protected int getListViewID()
    {
        return R.id.filedetail_list;
    }

    @Override
    protected int getContentViewID()
    {
        return R.layout.filedetail_activity;
    }
}
